package com.pig4cloud.pig.village.welfare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.village.api.dto.WelfareDTO;
import com.pig4cloud.pig.village.api.entity.Fund;
import com.pig4cloud.pig.village.api.entity.Welfare;
import com.pig4cloud.pig.village.fund.service.IFundService;
import com.pig4cloud.pig.village.welfare.mapper.WelfareMapper;
import com.pig4cloud.pig.village.welfare.service.IWelfareService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author limbray.li
 * @date 2021/6/19
 */
@Service
@RequiredArgsConstructor
public class WelfareServiceImpl extends ServiceImpl<WelfareMapper, Welfare> implements IWelfareService {
	@Resource
	private IFundService fundService;

	@Override
	public List<WelfareDTO> listDtoByFundId(Long id) {
		LambdaQueryWrapper<Welfare> query = Wrappers.lambdaQuery();
		query.eq(Welfare::getFundId, id);
		List<Welfare> welfareList = super.list(query);
		if (CollectionUtils.isEmpty(welfareList)) {
			return new ArrayList<>();
		}
		Fund fund = fundService.getById(id);
		List<WelfareDTO> results = welfareList.stream().map(e -> {
					WelfareDTO dto = new WelfareDTO();
					BeanUtil.copyProperties(e, dto);
					if (fund != null) {
						dto.setFundCode(fund.getFundCode());
						dto.setFundName(fund.getFundName());
					}
					return dto;
				}
		).collect(Collectors.toList());
		return results;
	}

	@Override
	public List<Welfare> listByFundId(Long id) {
		LambdaQueryWrapper<Welfare> query = Wrappers.lambdaQuery();
		query.eq(Welfare::getFundId, id);
		return super.list(query);
	}
	@Override
	public List<Welfare> listAutomousWelfare() {
		String fundCode = "autonomousFund";
		//获取自治金id
		Fund fund = fundService.getFundByCode(fundCode);
		if(fund == null){
			return new ArrayList<>();
		}
		LambdaQueryWrapper<Welfare> query = Wrappers.lambdaQuery();
		query.eq(Welfare::getFundId, fund.getId());
		return super.list(query);
	}
}
